var reviewAdd = {};
reviewAdd.methods = {};

$(function(){
    //星星
    reviewAdd.methods.reviewAddStars();

    // 限制文本域输入
    reviewAdd.methods.heartInfoArea();

    // 图片上传
    reviewAdd.methods.imgUpload();

    // 删除上传图片
    reviewAdd.methods.imgUploadDelete();

    // 图片切换
    reviewAdd.methods.PictureSwitching();

    // 左右高度
    // reviewAdd.methods.equalHeight();

    // 发表
    reviewAdd.methods.published();
})

//星星
reviewAdd.methods.reviewAddStars = function(){

    $(".shop-reviewAdd-stars i").each(function(){
        var starNum =  parseInt($(this).text());
        if(starNum < 1 || starNum > 10){
            $(this).text("");
            $(this).css("width",0);
            return;
        }else{
            var newStarNum = ((starNum*14.8)/74*100)/2 + "%";
            $(this).css("width",newStarNum);
            $(this).parents(".shop-reviewAdd-stars-box").find("span").text($(this).text()+"分");
            $(this).text("");
        }
    })
}

// 限制文本域输入
reviewAdd.methods.heartInfoArea = function(){
    $(".shop-reviewAdd-info-area").on("input propertychange", function() {
        var $this = $(this),
            _val = $this.val(),
            count = "";
        if (_val.length > 500) {
            $this.val(_val.substring(0, 500));
        }
        count = 500 - $this.val().length;
        $(".shop-reviewAdd-info-count").text(count);
    });
}

// 左右高度
// reviewAdd.methods.equalHeight = function(){
//     var rightCon = $(".shop-reviewAdd-con-right").height();
//     $(".shop-reviewAdd-con-left").height(rightCon);
// }

// 图片上传
reviewAdd.methods.imgUpload = function(){
    $(".shop-reviewAdd-input-file").find("input").on("change",function(){

        var s = $(this).val();
        var start = s.indexOf(".") + 1;
        var name = s.substring(start, s.length).toLowerCase();
        if (name != "jpg" && name != "png" && name != "gif" && name != "jpeg") {
            return;
        }
        $(this).prop('type','file');
        //获取文件
        var file = $(this)[0].files[0];
            
        //创建读取文件的对象
        var reader = new FileReader();

        //创建文件读取相关的变量
        var imgFile;

        //为文件读取成功设置事件
        reader.onload=function(e) {
            imgFile = e.target.result;
            $(".shop-reviewAdd-uploadImg").append("<a><img src=" + imgFile + " ><span></span></a>");
            Controller.Alert("图片上传成功", true, 1000);
            reviewAdd.methods.imgUploadNum();
            reviewAdd.methods.imgUploadDelete();
        };
        $(this).prop('type','text'); 
        $(this).prop('type','file');
        //正式读取文件
        if(file == null){
            return false;
        }else{
            reader.readAsDataURL(file);
        } 

    })
}

// 图片上传数量限制
reviewAdd.methods.imgUploadNum = function(){
    $(".shop-input-file-num span").text($(".shop-reviewAdd-uploadImg").find("a").length);
    if($(".shop-reviewAdd-uploadImg").find("a").length <= 4){
        $(".shop-reviewAdd-input-file").show();
    }else{
        $(".shop-reviewAdd-input-file").hide();
        return;
    }
}

// 删除上传图片
reviewAdd.methods.imgUploadDelete = function(){
    $(".shop-reviewAdd-uploadImg a span").on("click",function(){
        $(this).parents("a").remove();
        $(".shop-reviewAdd-uploadImgBig").hide();
        // reviewAdd.methods.equalHeight();
        reviewAdd.methods.imgUploadNum();
    })
}

// 图片切换
reviewAdd.methods.PictureSwitching = function () {
    $(".shop-reviewAdd-upload").on("click",".shop-reviewAdd-uploadImg a",function () {
        if ($(this).hasClass("shop-img-selected")) {
            $(this).removeClass("shop-img-selected");
            // reviewAdd.methods.equalHeight();
        } else {
            $(this).parents(".shop-reviewAdd-con-right .shop-reviewAdd-uploadImg").find("a").each(function () {
                $(this).removeClass("shop-img-selected");
            });
            $(this).addClass("shop-img-selected");
        }
        reviewAdd.methods.UpdateBigimg($(this));
    });
}

// 放大图
reviewAdd.methods.UpdateBigimg = function (obj) {
    var imgSelected = obj.parents(".shop-reviewAdd-con-right .shop-reviewAdd-uploadImg").find(".shop-img-selected");
    var bigImg = obj.parents(".shop-reviewAdd-con-right").find(".shop-reviewAdd-uploadImgBig");
    if(imgSelected.length == 1){
        bigImg.css("display", "block");
        bigImg.find("img").attr("src", imgSelected.find("img").attr("src"));
    }else{
        bigImg.css("display", "none");
    }
    // reviewAdd.methods.equalHeight();
}


// 发表
reviewAdd.methods.published = function () {
    $(".shop-reviewAdd-published").on("click",".shop-reviewAdd-published-btn", function () {
        var spanTips = "<span class='spanTips' style='position: absolute; top:84px; right:-40px; color:#e64545;'>*必填</span>";
        if($(".shop-reviewAdd-info-area").val()!== "" && $(".shop-reviewAdd-info-area").val().trim()!==""){
            Message.Show("提示", "提交评价", true, true, 5, "sureCancle", reviewAdd.methods.publishedEmpty, reviewAdd.methods.publishedCancel);
        }else{
            if($(".shop-reviewAdd-info").find("span").hasClass("spanTips")){
                return;
            }
            $(".shop-reviewAdd-info").append(spanTips);
        }  

    })

    $(".shop-reviewAdd-info-area").on("change", function () {
        $(".shop-reviewAdd-info").find(".spanTips").remove();
    })
}
// 再次发表前清空(确定按钮)
reviewAdd.methods.publishedEmpty = function () {
    $(".shop-reviewAdd-info-area").val("");
    $(".shop-reviewAdd-info-count").text("0");
    $(".shop-reviewAdd-uploadImg").html("");
    $(".shop-reviewAdd-input-file").show();
    $(".shop-reviewAdd-uploadImgBig").hide();
    $(".shop-input-file-num span").text("0");
    // reviewAdd.methods.equalHeight();
    return true;

}
// (取消按钮)
reviewAdd.methods.publishedCancel = function () {
    return true;
}